<?php

/**
 * 根据筛选条件获取职位信息
 * User: yaosj
 * Date: 15-10-10
 * Time: 下午2:41
 */
class GetConditionalCareer implements iController
{
    public function output($queryArgs)
    {
        //省职位表
        if (isset($queryArgs['province'])) {
            return $this ->getProvinceJob($queryArgs);
        }

        global $cmsdb;
        //国家职位表
        if (null == ($params = $this->parseCountryArgs($queryArgs)))
            return genOutput(1000);

        //返回当年的国考 TODO 临时调整 major_year方便查询
        $sql = 'SELECT * FROM `data_job_major` WHERE 1  ';

        //当前年份
        $returnV = require __DIR__ . '/code2name.config.php';
        $curYear = $returnV['current_year'];
        if (isset($curYear))
            $sql .= " AND major_year = {$curYear} ";

        //省份
        if (!empty($params['area_id'])) {
            $sql .= " AND area_id = {$params['area_id']}";
        }
        //学位
        if (isset($params['qualification'])) {
            $sql .= " AND qualification_flag_min <= {$params['qualification']} AND qualification_flag_max >=  {$params['qualification']}";
        }
        //应届毕业生
        if (isset($params['is_fresh'])) {
            $sql .= " AND is_fresh_graduated <= {$params['is_fresh']}";
        }
        //基层工作时间
        if (isset($params['junior_year'])) {
            $sql .= " AND min_junior_year_flag <= {$params['junior_year']}";
        }

        $targetMajor = strtoupper($params['major_code']);
        $tempItems = $cmsdb->getResults($sql);

        $resultItems = array();
        foreach ($tempItems as $item) {
            if (is_object($item))
                $item = (array)$item;

            //政治面貌,当有政治面貌限制，并且不符合职位需求的时候，跳过这个职位
            if (isset($params['political'])) {
                if ('不限' != $item['political_status'] && !strstr($item['political_status'], $params['political'])) {
                    continue;
                }
            }
            //如果四选一有要求
            if ($item['is_west_volunteer'] || $item['is_3_1']|| $item['is_college_officer'] || $item['is_special_teacher'] ) {
                //如果个人经历不是不限(没有传参数),则进行匹配
                if (isset($params['other_job']) &&  empty($item[$params['other_job']]))
                    continue;
            }

            //专业。如果code是all，则有效，如果code中有一个code是目标code的子字符串，则有效
            $code = $item['major_codes'];
            if ($code == 'all') {
                $resultItems[] = array(
                    'uniq_code' => $item['id'],
                    'department_job_code' => $item['department_job_code'],
                    'job_title' => $item['job_title'],
                    'department_name' => $item['department_name'],
                    'personnel_officer' => $item['personnel_officer'],
                    'enrollment_num' => $item['enrollment_num'],
                    'exist_num' => $item['exist_num']
                );
                continue;
            }
            $cr = explode(",", $code);
            foreach ($cr as $c) {
                //考虑可能是大类的情况
                if (strstr($targetMajor, $c) || strstr($c, $targetMajor)) {
                    $resultItems[] = array(
                        'uniq_code' => $item['id'],
                        'department_job_code' => $item['department_job_code'],
                        'job_title' => $item['job_title'],
                        'department_name' => $item['department_name'],
                        'personnel_officer' => $item['personnel_officer'],
                        'enrollment_num' => $item['enrollment_num'],
                        'exist_num' => $item['exist_num']
                    );
                    break;
                }
            }
        }
        $updatedHour = getdate(time())['hours'];
        if ($updatedHour < 10)
            $uTime = $date = date('Y-m-d' ,strtotime('-1 day')).' 15:30';
        else if ($updatedHour >= 16)
            $uTime = $date = date('Y-m-d',time()).' 15:30';
        else
            $uTime = $date = date('Y-m-d',time()).' 09:00';

        return genOutput(1, array('count' => count($resultItems),
            'updated_time' => $uTime,
            'career' => array_slice($resultItems, $params['page_index'] * $params['page_size'], $params['page_size'])));
    }

    private function getProvinceJob($queryArgs) {

        global $cmsdb;
        if (null == ($params = $this->parseProvinceArgs($queryArgs)))
            return genOutput(1000);

        $sql = 'SELECT * FROM `data_province_job_major` WHERE 1 ';
        //省份
        $sql .= " AND province = '{$queryArgs['province']}'";
        //地区
        if (isset($params['area']) && !strstr($params['area'],'不限')) {
            $sql .= " AND area = '{$params['area']}'";
        }
        //学位
        if (isset($params['qualification'])) {
            $sql .= " AND qualification_flag_min <= {$params['qualification']} AND qualification_flag_max >=  {$params['qualification']}";
        }

        //基层工作时间
        if (isset($params['junior_year'])) {
            $sql .= " AND min_junior_year_flag <= {$params['junior_year']}";
        }



        $targetMajor = strtoupper($params['major_code']);
        $tempItems = $cmsdb->getResults($sql);

        $resultItems = array();
        foreach ($tempItems as $item) {
            if (is_object($item))
                $item = (array)$item;

            //政治面貌,当有政治面貌限制，并且不符合职位需求的时候，跳过这个职位
            if (!empty($item['political_status']) && '不限' != $item['political_status']) {
                if ( empty($params['political']) || !strstr($item['political_status'], $params['political'])) {
                    continue;
                }
            }
            //是否是村官
            if (1 == $item['is_college_officer']) {
                if (!isset($params['other_job']) || $params['other_job'] != 'is_college_officer')
                    continue;
            }

            //是否是四项目人员
            if (1 == $item['is_one_of_four']) {
                if (!isset($params['is_one_of_four']) || $params['is_one_of_four'] != 1)
                    continue;
            }
            //性别
            if ('不限' != $item['sexual']) {
                if ($item['sexual'] != $params['sexual'])
                    continue;
            }
            //其它工作经历
            if (!empty($item['other_job']) &&  ('不限' != $item['other_job'])) {

                if ($queryArgs['province'] == 14) {
                    if ($item['other_job'] != '全国' && $params['other_job'] == 3)
                        continue;
                } else if ($params['other_job'] != $item['other_job'])
                    continue;
            }

            //专业。如果code是all，则有效，如果code中有一个code是目标code的子字符串，则有效
            $code = $item['major_codes'];
            if ($code == 'all') {
                $resultItems[] = array(
                    'uniq_code' => $item['id'],
                    'department_job_code' => $item['department_job_code'],
                    'job_title' => $item['job_title'],
                    'department_name' => $item['department_name'],
                    'enrollment_num' => $item['enrollment_num'],
                    'exist_num' => $item['exist_num']
                );
                continue;
            }
            $cr = explode(",", $code);
            foreach ($cr as $c) {
                //考虑可能是大类的情况
                if (strstr($targetMajor, $c) || strstr($c, $targetMajor)) {
                    $resultItems[] = array(
                        'uniq_code' => $item['id'],
                        'department_job_code' => $item['department_job_code'],
                        'job_title' => $item['job_title'],
                        'department_name' => $item['department_name'],
                        'enrollment_num' => $item['enrollment_num'],
                        'exist_num' => $item['exist_num']
                    );
                    break;
                }
            }
        }
        $updatedHour = getdate(time())['hours'];
        if ($updatedHour < 10)
            $uTime = $date = date('Y-m-d' ,strtotime('-1 day')).' 15:30';
        else if ($updatedHour >= 16)
            $uTime = $date = date('Y-m-d',time()).' 15:30';
        else
            $uTime = $date = date('Y-m-d',time()).' 09:00';

        return genOutput(1, array('count' => count($resultItems),
            'updated_time' => $uTime,
            'career' => array_slice($resultItems, $params['page_index'] * $params['page_size'], $params['page_size'])));

    }

    private function parseProvinceArgs($queryArgs)
    {
        if (!isset($queryArgs['junior_1']) || !isset($queryArgs['junior_2'])|| !isset($queryArgs['junior_0']) )
            return null;
        if ($queryArgs['junior_2'] != -1 && !isset($queryArgs['major_top']))
            return null;
        $argsArr = array();
        //如果没有地区信息，专业大类，返回参数错误
        $argsArr['area'] = trim($queryArgs['junior_1']);
        $argsArr['sexual'] = trim($queryArgs['junior_0']);
        $argsArr['major_code'] = trim($queryArgs['major_top']);
        $argsArr['qualification'] = intval($queryArgs['junior_2']);//学位的信息转化成学历
        if (!empty($queryArgs['major_upper']))
            $argsArr['major_code'] = trim($queryArgs['major_upper']);
        if (!empty($queryArgs['major']))
            $argsArr['major_code'] = trim($queryArgs['major']);
        //高级选项，对空值和默认值做处理
        if (!empty($queryArgs['senior_0']) && '不限' != $queryArgs['senior_0'])
            $argsArr['political'] = trim($queryArgs['senior_0']);
        if (isset($queryArgs['senior_1'])&& ''!=$queryArgs['senior_1']&& '-' != $queryArgs['senior_1'])
            $argsArr['junior_year'] = intval(trim($queryArgs['senior_1']));
        //TODO 针对不同的省份，处理不同的筛选条件
        $province = trim($queryArgs['province']);
        if (!empty($queryArgs['senior_2']) && '-' != $queryArgs['senior_2']) {
            if (in_array($province ,array(12,28))) {
                $argsArr['other_job'] = trim($queryArgs['senior_2']);
            }
            else {
                $argsArr['is_one_of_four'] = trim($queryArgs['senior_2']);
            }
        }
        //分页信息
        if (isset($queryArgs['page_index']))
            $argsArr['page_index'] = intval(trim($queryArgs['page_index']));
        else
            $argsArr['page_index'] = 0;
        if (!empty($queryArgs['page_size']))
            $argsArr['page_size'] = intval(trim($queryArgs['page_size']));
        else
            $argsArr['page_size'] = 200;
        return $argsArr;
    }

    private function parseCountryArgs($queryArgs)
    {
        if (!isset($queryArgs['area']) || empty($queryArgs['major_top']) || !isset($queryArgs['degree']))
            return null;
        $argsArr = array();
        $argsArr['is_fresh'] = empty($queryArgs['is_fresh']) ? 0 : 1;
        //如果没有地区信息，专业大类，返回参数错误
        $argsArr['area_id'] = intval($queryArgs['area']);
        $argsArr['major_code'] = trim($queryArgs['major_top']);
        $argsArr['qualification'] = intval($queryArgs['degree']);//学位的信息转化成学历
        if (!empty($queryArgs['major_upper']))
            $argsArr['major_code'] = trim($queryArgs['major_upper']);
        if (!empty($queryArgs['major']))
            $argsArr['major_code'] = trim($queryArgs['major']);
        //高级选项，对空值和默认值做处理
        if (!empty($queryArgs['political']) && '不限' != $queryArgs['political'])
            $argsArr['political'] = trim($queryArgs['political']);
        if (isset($queryArgs['junior_year'])&& ''!=$queryArgs['junior_year']&& '-' != $queryArgs['junior_year'])
            $argsArr['junior_year'] = intval(trim($queryArgs['junior_year']));
        if (!empty($queryArgs['other_job']) && '-' != $queryArgs['other_job'])
            $argsArr['other_job'] = trim($queryArgs['other_job']);
        //分页信息
        if (isset($queryArgs['page_index']))
            $argsArr['page_index'] = intval(trim($queryArgs['page_index']));
        else
            $argsArr['page_index'] = 0;
        if (!empty($queryArgs['page_size']))
            $argsArr['page_size'] = intval(trim($queryArgs['page_size']));
        else
            $argsArr['page_size'] = 200;
        return $argsArr;
    }
}